package com.fg.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.NoParameters;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.SessionAware;

import com.fg.cookie.CookieInfo;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport implements SessionAware, ParameterAware, NoParameters {

	protected Map<String, String> parameters;
	
	protected Map resultMap;
	
	protected Map session; 
	
	protected InputStream inputStream; 

	
	protected Integer member_seq;
	
	public static final String ERROR404 = "error404";
	public static final String ERROR500 = "error500";
	
	public static final String CERTIFICATION = "certification";
	public static final String SIGN_UP = "sign_up";
	
	public BaseAction() {
		resultMap = new HashMap();
	}
	
	@Override
	public void setParameters(Map parameters) {
		this.parameters = extractParameterMap(parameters);
	}	

	public Map<String, String> getParameters() {
		return parameters;
	}


	@Override
	public void setSession(Map session) {
		this.session = session;
		if (this.session.get("member_seq") == null) {
			try {
				this.session.put("member_seq", CookieInfo.getLoginSeqFromCookie(ServletActionContext.getRequest()));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try{
			member_seq = (Integer)this.session.get("member_seq");
		}catch (NullPointerException e) {
			member_seq = -1;
		}
		
	}
	public Map getSession() {
		return session;
	}
	public void setInputStream(String inputString) {
		try {
			this.inputStream = new ByteArrayInputStream(inputString.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	public InputStream getInputStream() {
		return inputStream;
	}
	
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	
	/**
	 * setInputStream
	 * @param inputMap(String
	 */
	public void setInputStream(Map<String, String> inputMap){
		JSONObject resultJSON = JSONObject.fromObject(inputMap);
		System.out.println(resultJSON.toString());
		setInputStream(resultJSON.toString());
	}
	
	/**
	 * setInputStream(Object(Model)
	 * @param inputMap(String
	 */
	public void setInputStream(Object model){
		JSONObject resultJSON = JSONObject.fromObject(model);
		System.out.println(resultJSON.toString());
		setInputStream(resultJSON.toString());
	}
	
	public void setInputStream(Exception e){
		JSONObject resultJSON = new JSONObject();
		resultJSON.element("status_code", "500");
		resultJSON.element("status_txt", "error");
		System.out.println(resultJSON.toString());
		setInputStream(resultJSON.toString());
	}
	

	/**
	 * 
	 * @param parameterMap
	 * @return
	 */
	public Map<String, String> extractParameterMap(Map parameterMap){
		parameters = new HashMap<String, String>();
		if(parameterMap.size()>0){
			Set keys = parameterMap.keySet();
			
			Iterator it = keys.iterator();
			
			while(it.hasNext()){
				String key = (String)it.next();
				String[] values = (String[])parameterMap.get(key);
				parameters.put(key, values[0]);
			}
		}
		return parameters;
	}


	public Map getResultMap() {
		return resultMap;
	}
	
	public int getMember_seq() {
		return member_seq;
	}
	


	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}
	
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
}
